<template>
  <div class="main">
        <p class="main_text">物流结算明细</p>
      <!-- 搜索 -->
      <div class="g-search">
        <!-- 时间选择区域 -->
        <div class="demo-date-picker">
          <div class="block">
            <span class="demonstration">付款时间:</span>
            <el-date-picker v-model="searchValue.startAt" type="date" placeholder="开始时间" @change="startTime"
              :clearable="false" :default-time="defaultTime(0, 0, 0)" />
            <span class="demonstration"
              style="display: inline-block; font-size: 12px; color: #898989; margin: 20px">至</span>
            <el-date-picker v-model="searchValue.endAt" type="date" placeholder="结束时间" @change="endTime"
              :clearable="false" :default-time="defaultTime1(23, 59, 59)" />
            &emsp;
            <span class="demonstration">物流公司:</span>
            <el-select v-model="searchValue.company" style="width: 250px">
              <el-option v-for="item in companyListData" :key="item.name" :label="item.name" :value="item.code" />
            </el-select>
            <span class="demonstration" style="margin-left: 15px">发票状态:</span>
            <el-select v-model="searchValue.status" style="width: 250px">
              <el-option label="未付款" :value="false" />
              <el-option label="已付款" :value="true" />
            </el-select>
          </div>
        </div>
        <!-- 底部按钮区域 -->
        <div class="bottom_btn">
          <el-button class="solid" @click="search">查询</el-button>
          <el-button class="hollow" @click="reset">重置</el-button>
        </div>
      </div>
    <!-- ############################################## 数据表格 ############################################## -->
    <div class="cyr-flex">
      <div class="mb20">
        <el-button type="primary"
          v-if="roleList.write && searchValue.company !== '' && searchValue.status === false && searchValue.startAt != '' && searchValue.endAt != ''"
          @click="handleBatch">批量确认付款</el-button>
        <el-button v-if="roleList.write" @click="handleExportExcel">导出明细</el-button>
      </div>
      <div class="cyr-flex-box">
      <el-table :data="tableData"  :show-overflow-tooltip="true" v-adaptive border>
        <el-table-column label="编号" type="index" width="80" />
        <el-table-column label="物流公司" prop="companyName"></el-table-column>
        <el-table-column label="物流订单" prop="logisticSn"></el-table-column>
        <el-table-column label="更新日期" prop="updateTime"></el-table-column>
        <el-table-column label="运费" prop="price"></el-table-column>
        <el-table-column label="是否付款">
          <template #default="scope">
            <span>{{ scope.row.settleStatus ? '是' : '否' }}</span>
          </template>
        </el-table-column>
      </el-table>
      <!-- ############################################## 分页 ############################################## -->
      <smartPager v-model:current-page="page" v-model:page-size="pageSize" :total="total"
        @change="handleCurrentChange" />
      </div>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import useAuthorize from '@/hooks/authorize';
import moment from 'moment';
import { ElMessage } from 'element-plus';
import { RequesLogisticsDetail, RequestBatchPay, RequestLogisticsCompany } from '@/api/path/index';
import { formatJson, formatTimeHan } from '@/utils';
import { exportJson2Excel } from '@/utils/excel';
let roleList = useAuthorize();

const page = ref(1);
const pageSize=ref(10)
const searchValue = ref({
  startAt: '',
  endAt: '',
  page: page.value,
  rows: 10,
  company: '',
  status: '',
});

const radio = ref(''); //单选按钮
const defaultTime = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
const defaultTime1 = (H: any, M: any, S: any) => {
  var now = new Date();
  now.setHours(H);
  now.setMinutes(M);
  now.setSeconds(S);
  return now;
};
/**
 * 点击重置
 */
const reset = () => {
  searchValue.value = {
    startAt: '',
    endAt: '',
    page: 1,
    rows: 10,
    company: '',
    status: '',
  };
  page.value=1
  initData(searchValue.value);
};
// 开始时间
const startTime = () => {
  searchValue.value.startAt.setHours(0);
  searchValue.value.startAt.setMinutes(0);
  searchValue.value.startAt.setSeconds(0);
  searchValue.value.startAt = moment(searchValue.value.startAt).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '';
};
// 结束时间
const endTime = () => {
  searchValue.value.endAt.setHours(23);
  searchValue.value.endAt.setMinutes(59);
  searchValue.value.endAt.setSeconds(59);
  searchValue.value.endAt = moment(searchValue.value.endAt).format('yyyy-MM-DD HH:mm:ss');
  radio.value = '0';
};
/**
 * 搜索点击事件
 */
const search = async () => {
  //时间是否存在
  if (searchValue.value.startAt !== '' && searchValue.value.endAt !== '') {
    if (moment(searchValue.value.startAt).diff(moment(searchValue.value.endAt)) >= 0) {
      ElMessage.error('开始时间不能晚于结束时间');
    } else {
      page.value = 1;
      //请求数据
      initData(searchValue.value);
    }
  } else {
    page.value = 1;
    initData(searchValue.value);
  }
};
const tableData = ref();
const total = ref(0);

const handleExportExcel = () => {
  const tHeader = ['编号', '物流公司', '物流订单', '更新日期', '运费', '是否付款']
  const filterVal = ['id', 'companyName', 'logisticSn', 'updateTime', 'price', 'settleStatus']
  const list = tableData.value.map((item, index) => {
    return {
      ...item,
      id: index,
      settleStatus: item.settleStatus ? '是' : '否'
    }
  })
  const data = formatJson(filterVal, list)
  exportJson2Excel(tHeader, data, `${formatTimeHan(new Date())}物流结算明细`, undefined, undefined,)
};
const handleBatch = () => {
  if (tableData.value.length === 0) return
  RequestBatchPay({
    startAt: searchValue.value.startAt,
    endAt: searchValue.value.endAt,
    company: searchValue.value.company,
    ids: tableData.value.map(item => item.id)
  }).then(res => {
    ElMessage.success(res.resultObj)
    initData(searchValue.value);

  })
}
function initData(search: any) {
  searchValue.value.page = page.value;
  searchValue.value.rows = pageSize.value;
  RequesLogisticsDetail(search).then((res) => {
    tableData.value = res.resultObj.list;
    total.value = res.resultObj.count;
  });
}
//物流公司列表
const companyListData = ref();
function companyList() {
  RequestLogisticsCompany().then((res) => {
    companyListData.value = res.resultObj;
  });
}
initData(searchValue.value);
companyList();
const handleCurrentChange = (e) => {
  // searchValue.value.page = e;
  initData(searchValue.value);
};
// const handleConfirmPay=(item)=>{
//   RequestBatchPay([item.id]).then(res=>{
//     console.log(res)
//   })
// }
</script>
<style lang="scss" scoped>
.main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  .content {
    width: 100%;
    background-color: #fff;
  }
}

.order {
  .box {
    width: 96%;
    padding: 20px;
    background: #ffffff;
    margin: 20px auto;
    display: flex;
    flex-wrap: wrap;

    .title {
      width: 100%;
      display: flex;

      .right {
        flex: 1;

        .el-steps {
          padding: 0 0 20px 0;
          border-bottom: 1px solid #b9b9b9;
        }

        .text {
          width: 100%;
          margin: 10px 0 25px 0;

          .shop {
            width: 100%;
            height: 30px;
            font-size: 18px;
            display: flex;
            align-items: center;
            justify-content: space-between;
            color: #3d3d3d;
            font-weight: bold;
            margin-top: 20px;
          }

          .time {
            display: inline-block;
            padding: 5px;
            background: #f5f9ff;
            color: #409eff;
          }

          .text1 {
            font-size: 14px;
            color: #ababab;
          }
        }
      }
    }

    .mes {
      display: flex;
      justify-content: space-between;
      flex-wrap: wrap;

      .info {
        width: 195px;
        font-size: 14px;
        color: #797979;
        margin-top: 15px;
      }

      .addremark {
        display: inline-block;
        color: #116cf8;
        margin-left: 10px;
        font-size: 12px;
      }
    }

    .user_play {
      width: 50%;
      font-size: 14px;

      .box1 {
        width: 3px;
        height: 15px;
        border: 0;
        background: #116cf8;
        margin-right: 10px;
      }

      .name {
        font-weight: bold;
        font-size: 15px;
      }

      .title {
        display: flex;
        border: 0;
        align-items: center;
        font-size: 15px;
        margin-bottom: 15px;

        .box1_other {
          background: #fff;
        }
      }

      .row {
        margin-top: 5px;

        .name1 {
          color: #b8b8b8;
        }
      }
    }

    .head_title {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      font-size: 15px;
      font-weight: bold;

      .box1 {
        width: 3px;
        height: 20px;
        background: #116cf8;
        margin-right: 10px;
      }
    }

    .logistics {
      margin-top: 20px;
    }

    .deposit {
      background: #fff;
      border: 0px;
      color: #409eff;
    }
  }

  .fristbox {
    margin-top: 0px;
  }
}
</style>